package com.practice.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.practice.common.result.PageResult;
import com.practice.common.result.Result;
import com.practice.mapper.user.TagMapper;
import com.practice.pojo.admin.dto.PageTagDTO;
import com.practice.pojo.admin.vo.TagVO;
import com.practice.pojo.user.entity.Tag;
import com.practice.service.admin.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;


@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper tagMapper;
    /**
     * 标签修改
     * @return
     */
    @Override
    public Result update(Tag tag) {
        tag.setDefaultData(null);
        tagMapper.updateById(tag);
        return Result.success();
    }
    /**
     * 标签添加
     * @return
     */
    @Override
    public Result add(Tag tag) {
        tag.setDefaultData("1");
        tagMapper.insert(tag);
        return Result.success();
    }

    /**
     * 标签列表全部
     * @return
     */
    @Override
    public Result selectAll() {
        List<Tag> tags = tagMapper.selectList(null);
        List<TagVO> tagVOS = BeanUtil.copyToList(tags, TagVO.class);
        return Result.success(tagVOS);
    }

    /**
     * 标签详情
     * @return
     */
    @Override
    public Result selectById(String id) {
        Tag tag = tagMapper.selectById(id);
        TagVO tagVO = BeanUtil.copyProperties(tag, TagVO.class);
        return Result.success(tagVO);
    }

    /**
     * 标签删除
     * @return
     */
    @Override
    public Result deleteById(String id) {
        tagMapper.deleteById(id);
        return Result.success();
    }
    /**
     * 标签列表分页
     * @return
     */
    @Override
    public Result page(PageTagDTO dto) {
        Long page = Long.valueOf(dto.getPage());
        Long limit = Long.valueOf(dto.getLimit());
        //开始分页
        IPage<Tag> tagPage = new Page<>(page,limit);
        //2.设置查询条件 模糊查询dto传入的TagName
        LambdaQueryWrapper<Tag> lqw = new LambdaQueryWrapper<>();
        lqw.like(Tag::getTagname,dto.getTagname());
        tagMapper.selectPage(tagPage,lqw);
        //3.属性赋值
        List<TagVO> tagVOS = BeanUtil.copyToList(tagPage.getRecords(), TagVO.class);
        //4.封装的PageResult
        PageResult pageResult = PageResult.builder().count(tagPage.getTotal())
                .totalPages(tagPage.getPages())
                .pageSize(tagPage.getSize())
                .currentPage(tagPage.getCurrent())
                .data(tagVOS)
                .build();
        return Result.success(pageResult);
    }

}
